<?php
namespace User\Controller;
use Common\Controller\MemberbaseController;
class FavoriteController extends MemberbaseController {
	
	
	protected $db;
	function _initialize(){
		parent::_initialize();
		$this->db=M("UserFavorite");
	}

	
	function index(){




			$map [uid] = $this->u[id];


			$count = $this->db->where ( $map )->count ();
			$page = $this->page ( $count, 15,I("post.page") );
			$data = $this->db->where ( $map )->limit ( $page->firstRow . ',' . $page->listRows )->order ( array (

				"id" => "DESC"
			) )->select ();


			foreach($data as $k=>$v){

				$info=D("Product")->where(array("id"=>$v["product_id"]))->field('content',true)->find();



				$info["l_id"]=$v["id"];
				$list[]=$info;




			}

		$this->assign("_list",$list);


			$this->display ();








	}
	
	function add(){
		
		
		
		$_GET[uid]=$this->u[id];


		$data=array(
			"uid"=>$this->u[id],
			"product_id"=>I("get.product_id")
		);
		$s=$this->db->where($data)->find();
		if($s){
			
			$this->error("已经收藏过了");
		}
		
		if ($this->db->create($data)) {
			 
		
			$id = $this->db->add($data);
			if ($id) {
				 
				 
				$this->success("收藏成功！");
			} else {
				$this->error("收藏失败！");
			}
		} else {
			$this->error($this->db->getError());
		}
		
		
		}
	
	function delete(){
		$id=I("get.id");
		$uid=$this->u[id];
		$result=$this->db->where(array('id'=>$id,'uid'=>$uid))->delete();
		if($result){
			$this->success("取消收藏成功！");
		}else {
			$this->error("取消收藏失败！");
		}
	}
}